package dfs;

import java.util.ArrayList;
import java.util.List;

/**
 * 回溯
 */
public class Solution {
    List<List<Integer>> result = new ArrayList<>();
    List<Integer> path = new ArrayList<>();

    /**
     * 77. 组合
     * @param n
     * @param k
     * @return
     */
    public List<List<Integer>> combine(int n, int k) {
        backtracking(n, k, 1);
        return result;
    }

    private void backtracking(int n, int k, int startIndex) {
        if (path.size() > k) return;

        if (path.size() == k) {
            result.add(new ArrayList<>(path));
            return;
        }
        for (int i = startIndex; i <= n - (k - path.size()) + 1; i++) {
            path.add(i);
            backtracking(n, k, i + 1);
            path.remove(path.size() - 1);
        }
    }

    /**
     * 216. 组合总和 III
     * @param k
     * @param n
     * @return
     */
    public List<List<Integer>> combinationSum3(int k, int n) {
        backtracking(k, n, 0, 0);
        return result;
    }

    private void backtracking(int k, int n, int startIndex, int sum) {
        if (sum > n || path.size() > k) {
            return;
        }
        if (sum == n && path.size() == k) {
            result.add(new ArrayList<>(path));
            return;
        }

        for (int i = startIndex; i < n - sum + 1; i++) {
            path.add(i);
            backtracking(k, n, i + 1, sum + i);
            path.remove(path.size() - 1);
        }
    }

    List<String> res = new ArrayList<>();
    String[] numString = {"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
    StringBuilder stringBuilder = new StringBuilder();
    /**
     * 17. 电话号码的字母组合
     * @param digits
     * @return
     */
    public List<String> letterCombinations(String digits) {
        if (digits == null || digits.length() == 0) {
            return res;
        }
        backtracking(digits, 0);
        return res;
    }
    private void backtracking(String digits, int index) {
        if (index >= digits.length()) {
            res.add(stringBuilder.toString());
            return;
        }
        int num = digits.charAt(index) - '0';
        String str = numString[num];
        for (int i = 0; i < str.length(); i++) {
            stringBuilder.append(str.charAt(i));
            backtracking(digits, index + 1);
            stringBuilder.deleteCharAt(stringBuilder.length() - 1);
        }
    }

    /**
     * 39. 组合总和
     * @param candidates
     * @param target
     * @return
     */
    public List<List<Integer>> combinationSum(int[] candidates, int target) {
        backtracking(candidates, target, 0, 0);
        return result;
    }
    private void backtracking(int[] candidates, int target, int sum, int index) {
        if (sum > target || index >= candidates.length) {
            return;
        }
        if (sum == target) {
            result.add(new ArrayList<>(path));
        }

        for (int i = index; i < candidates.length; i++) {
            path.add(candidates[i]);
            backtracking(candidates, target, sum + candidates[i], i);
            path.remove(path.size() - 1);
        }
    }

    /**
     * 46. 全排列
     */
    boolean[] used;
    public List<List<Integer>> permute(int[] nums) {
        used = new boolean[nums.length];
        backtracking(nums);
        return result;
    }

    private void backtracking(int[] nums) {
        if (path.size() == nums.length) {
            result.add(new ArrayList<>(path));
            return;
        }

        for (int i = 0; i < nums.length; i++) {
            if (!used[i]) {
                path.add(nums[i]);
                used[i] = true;
                backtracking(nums);
                used[i] = false;
                path.remove(path.size() - 1);
            }
        }
    }

}
